package com.prp.struts.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.prp.service.ServiceFactory;

public class LoginAction extends ActionSupport {

	/**
	 * @return
	 */
	private String username;
	private String password;
	private boolean cookie;
	
	public String getUsername(){
		return this.username;
	}
	
	public void setUsername(String username){
		this.username = username;
	}
	
	public String getPassword(){
		return this.password;
	}
	
	public void setPassword(String password){
		this.password = password;
	}
	
	public boolean getCookie(){
		return this.cookie;
	}
	
	public void setCookie(boolean cookie){
		this.cookie = cookie;
	}
	
	public void validate(){
		if(username == null || username.equals("")){
			this.addFieldError("username.error", 
					getText("_UserName")+getText("_error.empty"));
		}
		
		if(password == null || password.equals("")){
			this.addFieldError("password.error", 
					getText("_PassWord")+getText("_error.empty"));
		}
	}
	
	public String execute() {
		
		if(ServiceFactory.getUserManage().login(username, password)){
			Map session = ActionContext.getContext().getSession();
			Object o = session.get("GOING_TO");
			if (o != null)
				return "goto";
			
			
			
			return SUCCESS;
		}			
		else{
			this.addActionError(this.getText("_error.username"));
			return INPUT;
		}
	}
}